var id = 0//当前组件id
var count = 0//道具切换计数
var hidden_count = 0//隐藏成就计数
const app = getApp()
Page({
  data: {
    buttons: [{ id: 1, name: '应急包',img:'../../image/bag.png',select:true}, { id: 2, name: '医疗箱',img:'../../image/medical.png',select:false},{ id: 3, name: '食物箱',img:'../../image/box.png',select:false},{ id: 4, name: '钱',img:'../../image/money.png',select:false},{ id: 5, name: '手机',img:'../../image/phone.png',select:false}],//按钮基本属性
    text: '请选择1个',
    tip:'你还未选中道具'
  },
  onLoad(){
    id = 0,count = 0,hidden_count = 0
  },
  prop: function() {
      let that = this;
      if (id && id != 4) {//如果id不为0
        app.globalData.propId = id   
        wx.navigateTo({url: '../../pages/game/game?id='+ 2})//传递id到game.js  
      }else if(id == 4){
        hidden_count++
       if(hidden_count == 5){//连续点5次给个隐藏成就
        let hidden = false
        let value = wx.getStorageSync('achievement')
        let ach = {"achievementId":99,"achievement":"钱虽然不能让你在被困的时候得到帮助，但你获得了心理安慰。","img":"http://m.qpic.cn/psc?/V53ixDRl2lGNfk1u0r2u1lmtnp3AJ6wG/ruAMsa53pVQWN7FLK88i5j6bGkhvozRxZYL*7.BGFS7SQCaq.tg40SAVFPcWUhMHWRKEhtQ1SDtdPpTBxk9Gpv0xfSDXPDmkS.yY2e*P9w4!/b&bo=gACAAAAAAAADByI!&rf=viewer_4","name":"富贵险中求(隐藏成就)","source":"你在选择钱后累积点了5下","hidden":true}
        //判断ach-99以及是否有其它成就存在，如果不存在则添加，重点是其它成就如果不存在但没判断会报错，无法用find方法
        value?value.find(item=>item.achievementId == 99)?hidden = true:value.push(ach):(value = [],value.push(ach))
        if(hidden == false){
          wx.setStorageSync("achievement", value),//保存到本地
          this.setData({
            tip: '成就：富贵险中求',
            show: 1
        })}
        setTimeout(function () {
          that.setData({
          show: 0
        })
       }, 2000)
      }else{
        this.setData({
          tip: '嗯？这都啥时候了还想着拿钱呢？',
          show: 1
     })
        setTimeout(function () {
          that.setData({
          show: 0
     })
       }, 2000)
  }
      }else{
        //提示未选择 
        this.setData({
            tip: '你还未选中道具',
            show: 1
        })
        setTimeout(function () {
          that.setData({
            show: 0
        })
      }, 2000)
    }
  },
  buttonSelect: function (e) {
    id = e.currentTarget.dataset.id
    this.setData({
      text: '开始游戏'
  })
},
  left:function() {
    //把按钮的当前项设置为不可见，下一项可见
    if(count > 0){
      id = 0//id清空
      this.data.buttons[count].select = false
      count--
      this.data.buttons[count].select = true
      this.setData({
        text: '请选择1个',
        buttons: this.data.buttons,//更新数据，并部署到页面中
    })
    }
  },
  right:function() {
    if(count < 4){
      id = 0
      this.data.buttons[count].select = false
      count++
      this.data.buttons[count].select = true
      this.setData({
        text: '请选择1个',
        buttons: this.data.buttons
    })
    }
  }
})
